package leetcode.BinarySearch;

public class FirstBadVersion {

    public static int firstBadVersion(int n) {
        if(n==0||n==1){
            return 1;
        }
        if(isBadVersion(1)){
            return 1;
        }
        int left = 1;
        int right = n;

        while (left < right) {
            int mid = left + (right - left) / 2;
            if (!isBadVersion(mid) && isBadVersion(mid + 1)) {
                return mid+1;
            } else if (!isBadVersion(mid) && !isBadVersion(mid + 1)) {
                left = mid;
            } else {
                right = mid;
            }
        }
        return -1;


    }

    public static int betterFirstBadVersion(int n) {
        if(isBadVersion(1))
            return 1;

        int start = 1;
        int end = n;
        int mid;
        while (start < end) {
            mid = start/2 + end/2;
            if(isBadVersion(mid)){
                end = mid ;
            }else {
                start = mid + 1;

            }
        }
        return start;
    }

    private static boolean isBadVersion(int n) {
        if (n >= 1702766719) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(firstBadVersion(2126753390));

    }



}
